package com.meeruu.sg.account.core.domain.biz;

import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.meeruu.boot.ucs.annotation.NamedCache;
import com.meeruu.boot.ucs.datasource.IUcsCache;
import com.meeruu.common.model.error.BaseCode;
import com.meeruu.common.model.page.PageParam;
import com.meeruu.common.model.page.PageResult;
import com.meeruu.sg.account.core.api.exception.AccountException;
import com.meeruu.sg.account.core.api.exception.ErrorCode;
import com.meeruu.sg.account.core.domain.mapper.ConfigUserWithdrawMapper;
import com.meeruu.sg.account.core.domain.model.ConfigUserWithdraw;
import com.meeruu.sg.account.core.domain.model.cache.CacheKeys;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ConfigUserWithdrawBiz{

    @Resource
    private ConfigUserWithdrawMapper configUserWithdrawMapper;

    @NamedCache("default")
    private IUcsCache ucsCache;

    public ConfigUserWithdraw queryConfig(Integer levelId) {
        if (levelId == null) {
            throw new AccountException(ErrorCode.PARAM_ERROR);
        }
        List<ConfigUserWithdraw> userWithdraws = ucsCache.get(CacheKeys.KEY_USER_WITHDRAW_CONFIG,
                                                              new TypeReference<List<ConfigUserWithdraw>>(){}.getType());
        if (CollectionUtils.isEmpty(userWithdraws)) {
            throw new AccountException(ErrorCode.SYS_EXCEPTION);
        }

        Optional<ConfigUserWithdraw> config = userWithdraws.stream()
                .filter(x -> x.getLevelId().equals(levelId))
                .findFirst();

        return config.isPresent() ? config.get() : null;
    }

    
    public PageResult<ConfigUserWithdraw> queryPage(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<ConfigUserWithdraw> withdraws = configUserWithdrawMapper.selectAll();

        long total = configUserWithdrawMapper.count();
        PageResult<ConfigUserWithdraw> pageResult = new PageResult<>(
                pageParam.getPage(), pageParam.getPageSize(), total);
        if (CollectionUtils.isEmpty(withdraws)) {
            pageResult.setData(new ArrayList<>());
        } else {
            pageResult.setData(withdraws);
        }
        return pageResult;
    }


    public void addConfig(ConfigUserWithdraw withdraw) {
        if (withdraw == null || withdraw.getLevelId() == null ||
                withdraw.getDateType() == null || withdraw.getWithdrawTimes() == null) {
            throw new AccountException(ErrorCode.PARAM_ERROR);
        }
        withdraw.setCreateTime(new Date());
        withdraw.setUpdateTime(new Date());
        configUserWithdrawMapper.insert(withdraw);
    }

    public void modifyConfig(ConfigUserWithdraw withdraw) {
        if (withdraw == null || withdraw.getId() == null) {
            throw new AccountException(BaseCode.PARAM_ERROR.getCode(), "参数为空或ID为空");
        }
        configUserWithdrawMapper.updateByPrimaryKey(withdraw);
    }

    public void removeConfig(Integer id) {
        if (id == null) {
            throw new AccountException(BaseCode.PARAM_ERROR.getCode(), "ID为空");
        }
        configUserWithdrawMapper.deleteByPrimaryKey(id);
    }

    public List<ConfigUserWithdraw> queryAll() {
        return configUserWithdrawMapper.selectAll();
    }
}
